home *** CD-ROM | disk | FTP | other *** search
- Path: kom22.ethz.ch!conrad
- From: conrad@kom22.ethz.ch (Christian Conrad)
- Newsgroups: comp.lang.c++
- Subject: Programmed-in relationships (Stroustrup Example)
- Date: 16 Feb 1996 11:34:16 GMT
- Organization: Swiss Federal Institute of Technology (ETHZ)
- Message-ID: <4g1q3o$scu@elna.ethz.ch>
- NNTP-Posting-Host: kom22-e.ethz.ch
- X-Newsreader: TIN [version 1.2 PL1]
-
- Hello,
-
- I am having some trouble with programmed-in relationships, especially
- with the delegation notion.
-
- In the following piece of code, only the 2 constructor functions must
- be visible for the application programmer (all the rest is implementation
- detail and must not be accessible...). I would like to access B::f() by
- delegation through A::p in the constructor of class A.
- --->Compiler output: B::f() is not accessible from A::A(B*).
-
- I see only one solution:
- - declare B::f() as a friend in both A and B classes,
- BUT what prevents the "user" to create a C class with a
- friend f() declaration, and then to "abuse" of my
- function f() ?
-
-
- A similar example is provided in the sec. ed. of the Stroustrup's C++ ...
- (page 421).
-
- class B {
- void f() {} // current implementation of the function
- public:
- B();
- };
-
- class A {
- B *p; // delegation through p
- public:
- A(B *pb);
- };
-
- A::A(B *pb) {
- p=pb;
- p->f(); // Not possible to access the B object...
- };
-
- main()
- {
- B b;
- A a(&b);
- };
-
- Is my "reluctance" to declare f() as a friend justified or not in this case ?
- As I am a beginner in C++ (and also OOP), I would appreciate any advice
- to improve this design or to correct it if it is totally wrong !
-
- Thanks,
- Christian
-
- --
-
- --------------------------- % % % % % ---------------------------
- %
- Christian CONRAD %
- % Tel. (+41) 1 / 632 7015
- ETH-Zurich % Fax (+41) 1 / 632 1035
- Technische Informatik und % E-Mail: conrad@tik.ee.ethz.ch
- Kommunikationsnetze (TIK) %
- %
- ---------------------------- % % % % % ---------------------------
-
-
-
-
-
-
-
-